﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Controls.Primitives;
using System.Windows.Media.Effects;
using System.Windows.Media;
using System.Windows.Shapes;

namespace RS.ZhaoXiWidgets.Controls
{
    public class ZXCard1 : ContentControl
    {
        static ZXCard1()
        {
            DefaultStyleKeyProperty.OverrideMetadata(typeof(ZXCard1), new FrameworkPropertyMetadata(typeof(ZXCard1)));
        }



        public string HeaderDes
        {
            get { return (string)GetValue(HeaderDesProperty); }
            set { SetValue(HeaderDesProperty, value); }
        }

        public static readonly DependencyProperty HeaderDesProperty =
            DependencyProperty.Register("HeaderDes", typeof(string), typeof(ZXCard1), new PropertyMetadata(string.Empty));




        public object HeaderContent
        {
            get { return (object)GetValue(HeaderContentProperty); }
            set { SetValue(HeaderContentProperty, value); }
        }

        public static readonly DependencyProperty HeaderContentProperty =
            DependencyProperty.Register("HeaderContent", typeof(object), typeof(ZXCard1), new PropertyMetadata(null));


        protected override void OnRender(DrawingContext drawingContext)
        {
            base.OnRender(drawingContext);

            //获取控件的实际尺寸
            double width = ActualWidth;
            double height = ActualHeight;

            var borderColor = (Color)ColorConverter.ConvertFromString("#255774");

            // 定义画笔
            Pen borderPen = new Pen(new SolidColorBrush(borderColor), 1);
            StreamGeometry borderGeometry = new StreamGeometry();
            borderGeometry.FillRule = FillRule.EvenOdd;


            //第0个点到第1个点的距离
            int point0ToPoint1Distance = 10;

            //第3个点到第4个点的高度
            int point3ToPoint4Distance = 50;

            //第4个点到第5个点的高度 和第2点到第3个点的高度一样
            int point4ToPoint5Height = 5;

            //第4个点到第5个点的距离 和第2个点到第3个点的距离一样
            int point4ToPoint5Distance = 6;

            //第5个点都第6个点的距离
            int point5ToPoint6Distance = 60;

            //第6个点都第7个点的高度
            int point6ToPoint7Height = 15;

            //第6个点都第7个点的距离
            int point6ToPoint7Distance = 8;

            //第7个点都第8个点的距离
            int point7ToPoint8Distance = 5;

            //第9个点都第10个点的高度
            int point9ToPoint10Height = 15;

            //第9个点都第10个点的距离
            int point9ToPoint10Distance = 15;

            //第0个点都第12个点的距离
            int point0ToPoint12Height = 10;
            Point point0 = new Point(0, 0);
            Point point1 = new Point(point0ToPoint1Distance, 0);
            Point point2 = new Point(width - point4ToPoint5Distance - point3ToPoint4Distance - point4ToPoint5Distance - point5ToPoint6Distance - point6ToPoint7Distance - point7ToPoint8Distance, 0);
            Point point3 = new Point(width - point3ToPoint4Distance - point4ToPoint5Distance - point5ToPoint6Distance - point6ToPoint7Distance - point7ToPoint8Distance, 0 + point4ToPoint5Height);
            Point point4 = new Point(width - point4ToPoint5Distance - point5ToPoint6Distance - point6ToPoint7Distance - point7ToPoint8Distance, 0 + point4ToPoint5Height);
            Point point5 = new Point(width - point5ToPoint6Distance - point6ToPoint7Distance - point7ToPoint8Distance, 0);
            Point point6 = new Point(width - point6ToPoint7Distance - point7ToPoint8Distance, 0);
            Point point7 = new Point(width - point7ToPoint8Distance, 0 + point6ToPoint7Height);
            Point point8 = new Point(width, 0 + point6ToPoint7Height);
            Point point9 = new Point(width, height - point9ToPoint10Height);
            Point point10 = new Point(width - point9ToPoint10Distance, height);
            Point point11 = new Point(0, height);
            Point point12 = new Point(0, 0 + point0ToPoint12Height);

            using (StreamGeometryContext ctx = borderGeometry.Open())
            {
                // 左上角装饰
                ctx.BeginFigure(point1, false, false);
                ctx.LineTo(point2, true, true);
                ctx.LineTo(point3, true, true);
                ctx.LineTo(point4, true, true);
                ctx.LineTo(point5, true, true);
                ctx.LineTo(point6, true, true);
                ctx.LineTo(point7, true, true);
                ctx.LineTo(point8, true, true);
                ctx.LineTo(point9, true, true);
                ctx.LineTo(point10, true, true);
                ctx.LineTo(point11, true, true);
                ctx.LineTo(point12, true, true);
                ctx.LineTo(point1, true, true);

                //画上面的横线
                var horizontalLinePoint1 = new Point(point3.X + 5, 0);
                var horizontalLinePoint2 = new Point(point4.X - 5, 0);
                ctx.BeginFigure(horizontalLinePoint1, false, false);
                ctx.LineTo(horizontalLinePoint2, true, true);
            }
            // 最后绘制主边框
            drawingContext.DrawGeometry(null, borderPen, borderGeometry);


            var trianglePoint1 = new Point(width, point9.Y + 5);
            var trianglePoint2 = new Point(width, height);
            var trianglePoint3 = new Point(point10.X + 5, height);
            // 创建三角形的路径几何图形
            StreamGeometry triangleGeometry = new StreamGeometry();
            using (StreamGeometryContext ctx = triangleGeometry.Open())
            {
                //画小三角
                ctx.BeginFigure(trianglePoint1, true, true);
                ctx.LineTo(trianglePoint2, true, false);
                ctx.LineTo(trianglePoint3, true, false);
            }

          
            // 使用纯色画刷填充
            SolidColorBrush solidBrush = new SolidColorBrush(borderColor);
            // 绘制三角形
            drawingContext.DrawGeometry(solidBrush, borderPen, triangleGeometry);

            //获取2个圆心的默认位置
            var circleCenter1 = new Point(40, 0);
            var circleCenter2 = new Point(0, 40);
            //圆点半径
            var circleRadius = 3;
            //追光尾巴长度
            var glowLineDistance = 20;

            var color = (Color)ColorConverter.ConvertFromString("#267985");
            // 绘制中心实心圆点
            drawingContext.DrawEllipse(
                new SolidColorBrush(color),
                new Pen(new SolidColorBrush(color), 1),
               circleCenter1,
                circleRadius,
                circleRadius
            );

            // 绘制中心实心圆点
            drawingContext.DrawEllipse(
                new SolidColorBrush(color),
                new Pen(new SolidColorBrush(color), 1),
               circleCenter2,
                circleRadius,
                circleRadius
            );

            // 创建渐变画刷
            var stops = new GradientStopCollection();
            stops.Add(new GradientStop(Colors.Transparent, 0.0));
            stops.Add(new GradientStop(color, 1));

            //第1个圆点的追光
            // 创建线性渐变画刷
            var gradientBrush = new LinearGradientBrush(stops)
            {
                StartPoint = new Point(0, 0.5),
                EndPoint = new Point(1, 0.5),
                MappingMode = BrushMappingMode.RelativeToBoundingBox
            };

            // 创建画笔
            var gradientPen = new Pen(gradientBrush, 3);
            drawingContext.DrawLine(
                gradientPen,
                new Point(circleCenter1.X - circleRadius - glowLineDistance, 0),
                new Point(circleCenter1.X - circleRadius, 0)
            );

            // 第2个圆点的追光
            // 创建线性渐变画刷
            gradientBrush = new LinearGradientBrush(stops)
            {
                StartPoint = new Point(0.5, 0),
                EndPoint = new Point(0.5, 1),
                MappingMode = BrushMappingMode.RelativeToBoundingBox
            };

            // 创建画笔
            gradientPen = new Pen(gradientBrush, 3);
            drawingContext.DrawLine(
                gradientPen,
                new Point(0, circleCenter2.Y - circleRadius - glowLineDistance),
                new Point(0, circleCenter2.Y - circleRadius)
            );

        }
        public override void OnApplyTemplate()
        {
            base.OnApplyTemplate();
        }
    }
}
